<?php

/**
 * Created by Reliese Model.
 */

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;

/**
 * Class User
 *
 * @property int $id
 * @property int|null $p_id
 * @property string|null $account
 * @property string|null $name
 * @property string|null $sex
 * @property Carbon|null $birthday
 * @property string|null $password
 * @property int|null $wxid
 * @property string|null $icon
 * @property int|null $worker_id
 * @property int|null $branch_id
 * @property string|null $customer_id
 * @property int|null $fdd_cert_status
 * @property int|null $fdd_status
 * @property string|null $verified_serialno
 * @property string|null $pro_code
 * @property string|null $city_code
 * @property string|null $area_code
 * @property string|null $address
 * @property string|null $email
 * @property string|null $qq_num
 * @property int|null $wx_num
 * @property string|null $inv_code
 * @property string|null $level
 * @property int|null $user_status
 * @property string|null $contact_manner
 * @property string|null $id_number
 * @property string|null $id_type
 * @property int $is_first
 * @property string|null $remark
 * @property Carbon|null $update_time
 * @property Carbon|null $create_time
 * @property int|null $id_types
 * @property int|null $old_id
 * @property int|null $sign_num
 * @property string|null $xcx_open_id
 * @property int|null $is_update_pwd
 * @property string|null $unionid
 * @property string|null $public_open_id
 * @property int|null $is_receive
 *
 * @property CustArea|null $cust_area
 * @property Branch|null $branch
 * @property Worker|null $worker
 * @property Collection|BankCard[] $bank_cards
 * @property Collection|ExcelFile[] $excel_files
 * @property Collection|Image[] $images
 * @property Collection|MassageChangeRecord[] $massage_change_records
 * @property Collection|MessageRecord[] $message_records
 * @property Collection|Order[] $orders
 * @property Collection|SystemOrderRecord[] $system_order_records
 * @property Collection|Tag[] $tags
 * @property Collection|Worker[] $workers
 *
 * @package App\Models
 */
class User extends Model
{
	protected $table = 'user';

	protected $casts = [
		'p_id' => 'int',
		'wxid' => 'int',
		'worker_id' => 'int',
		'branch_id' => 'int',
		'fdd_cert_status' => 'int',
		'fdd_status' => 'int',
		'wx_num' => 'int',
		'user_status' => 'int',
		'is_first' => 'int',
		'id_types' => 'int',
		'old_id' => 'int',
		'sign_num' => 'int',
		'is_update_pwd' => 'int',
		'is_receive' => 'int'
	];

	protected $dates = [
		'birthday'
	];

	protected $hidden = [
		'password'
	];

	protected $fillable = [
		'p_id',
		'account',
		'name',
		'sex',
		'birthday',
		'password',
		'wxid',
		'icon',
		'worker_id',
		'branch_id',
		'customer_id',
		'fdd_cert_status',
		'fdd_status',
		'verified_serialno',
		'pro_code',
		'city_code',
		'area_code',
		'address',
		'email',
		'qq_num',
		'wx_num',
		'inv_code',
		'level',
		'user_status',
		'contact_manner',
		'id_number',
		'id_type',
		'is_first',
		'remark',
		'id_types',
		'old_id',
		'sign_num',
		'xcx_open_id',
		'is_update_pwd',
		'unionid',
		'public_open_id',
		'is_receive'
	];

    protected $attributes = [
        'is_first' => '-1', // -1是新用户未开单 1是新用户已开单  2是老用户
        'user_status' => '1', // 1正常  -1已封禁
    ];

    public function pro()
    {
        return $this->belongsTo(CustArea::class, 'pro_code', 'area_code');
    }
    public function city()
    {
        return $this->belongsTo(CustArea::class, 'city_code', 'area_code');
    }
    public function area()
    {
        return $this->belongsTo(CustArea::class, 'area_code', 'area_code');
    }
    public function branch()
    {
        return $this->belongsTo(Branch::class);
    }

    public function worker()
    {
        return $this->belongsTo(Worker::class);
    }

    public function bank_cards()
    {
        return $this->hasMany(BankCard::class);
    }

    public function message_records()
    {
        return $this->hasMany(MessageRecord::class);
    }

    public function orders()
    {
        return $this->hasMany(Order::class);
    }

    public function system_order_record()
    {
        return $this->hasOne(SystemOrderRecord::class);
    }

    public function tags()
    {
        return $this->belongsToMany(Tag::class, 'user_tag')
            ->withPivot('id')
            ->withTimestamps();
    }

    public function workers()
    {
        return $this->belongsToMany(Worker::class)
            ->withPivot('id', 'old_worker_id', 'state', 'remark')
            ->withTimestamps();
    }

    public function images()
    {
        return $this->hasMany(Image::class, 'user_id');
    }

    public function reocrd_images()
    {
        return $this->hasMany(Image::class, 'record_user_id');
    }
}
